/**
 * File: ConnectionPool.h
 * Created Time: 2024-12-30
 * Author: chugehehe (3321592850@qq.com)
 */

#ifndef CONNECTION_POOL_H
#define CONNECTION_POOL_H

#include "MySQLConnection.h"
#include <memory>
#include <string>
#include <vector>
#include <mutex>
#include <condition_variable>

using std::string;
using std::unique_ptr;
using std::shared_ptr;
using std::vector;
using std::mutex;
using std::condition_variable;

class ConnectionPool {
public:
    ConnectionPool(size_t maxConnSize, const string &host, const string &user, const string &password, const string &database);
    shared_ptr<MySQLConnection> getConnection(); // 获取连接
    void releaseConnection(shared_ptr<MySQLConnection> &conn); // 释放连接

private:
    size_t _maxConnSize; // 最大连接数
    vector<shared_ptr<MySQLConnection>> _conns; // 连接池
    vector<shared_ptr<MySQLConnection>> _idleConns; // 空闲连接池
    mutex _mutex; // 互斥锁
    condition_variable _cond; // 条件变量
};

#endif